package com.pictures;


import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.ImageView;

/*
 *  Procesarea propriuzisa a imaginii si afisarea,
 *  Imaginea este primita prin Intent de la clasa responsabila de
 *  obtinerea imaginii, procesarea se face pe fiecare pixel.
 *  
 *  Obs: Imaginea cu sigla Android este ceea ce se primeste de la 
 *  emulator, daca se ruleaza pe device, va primi exact imaginea.
 *  
 */

public class ShowChanged extends Activity {
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.change);
		
		// Obiectul in care se afiseaza
		ImageView mIV = (ImageView) findViewById(R.id.imagine);

		Bitmap mBitmap = getIntent().getParcelableExtra("image23");
		
	//  Daca se ruleaza programul pe emulator, ca sa se vada mai bine diferentele
	//	Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),
	//			   R.drawable.picview);
		
		
		int picw  = mBitmap.getWidth();
		int pich = mBitmap.getHeight();
		int[] pix = new int[picw * pich];
		int deg = 200;
		
		// Se obtin pixelii din imagine
		mBitmap.getPixels(pix, 0, picw, 0, 0, picw, pich);
		
		// Se obtine componentele pixelului R, G, B  prin shiftare de biti
		// apoi se normalizeaza valorile
		int RY, GY, BY, RYY, GYY, BYY, R, G, B, Y;
		   double angle = (3.14159d * (double)deg) / 180.0d;
		   int S = (int)(256.0d * Math.sin(angle));
		   int C = (int)(256.0d * Math.cos(angle));

		   for (int y = 0; y < pich; y++)
		   for (int x = 0; x < picw; x++)
		      {
			   	  int index = y * picw + x;
			      int r = (pix[index] >> 16) & 0xff;
			      int g = (pix[index] >> 8) & 0xff;
			      int b = pix[index] & 0xff;
			      RY = ( 70 * r - 59 * g - 11 * b) / 100;
			      GY = (-30 * r + 41 * g - 11 * b) / 100;
			      BY = (-30 * r - 59 * g + 89 * b) / 100;
			      Y  = ( 30 * r + 59 * g + 11 * b) / 100;
			      RYY = (S * BY + C * RY) / 256;
			      BYY = (C * BY - S * RY) / 256;
			      GYY = (-51 * RYY - 19 * BYY) / 100;
			      R = Y + RYY;
			      R = (R < 0) ? 0 : ((R > 255) ? 255 : R);
			      G = Y + GYY;
			      G = (G < 0) ? 0 : ((G > 255) ? 255 : G);
			      B = Y + BYY;
			      B = (B < 0) ? 0 : ((B > 255) ? 255 : B);
			      pix[index] = 0xff000000 | (R << 16) | (G << 8) | B;
		      }
		   
		   Bitmap bm = Bitmap.createBitmap(pix, picw, pich, Bitmap.Config.RGB_565);

		   mIV.setImageBitmap(bm);

	
	}
}
